import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * @author Wang Chen Peng
 * @date 2019/01/10
 * describe:
 */
public class TestMap {


    public static void main(String[] args) {
        List<User> list = initUser();
        long l = testMap(list, 999);
        long l1 = testList(list, 999);
    }

    private static long testList(List<User> list, long target) {
        long start = System.currentTimeMillis();
        for (User user : list) {
            if (target == user.getId()) {
                System.out.println(System.currentTimeMillis() - start + "  list");
                return user.getId();
            }
        }
        return 9999;
        /*list.forEach(user -> {
            if (target == user.getId()) {
                System.out.println(user.getId());
                System.out.println(System.currentTimeMillis() - start + "  list");
                return ;
            }
        });*/
    }

    private static long testMap(List<User> list,long target) {
        long start = System.currentTimeMillis();
        HashMap<Long, User> map = new HashMap<>(200);
        for (User user : list) {
            if (map.containsKey(target)){
                User user1 = map.get(target);
                System.out.println(System.currentTimeMillis() - start + "  map");
                return user1.getId();
            }
            map.put(user.getId(),user );
        }
       return 999999;
        //System.out.println(System.currentTimeMillis() - start + "  map");
    }

    private static List<User> initUser() {
        ArrayList<User> users = new ArrayList<>();
        for (int i = 0; i < 99999; i++) {
            User user = new User(i, "" + i, "" + i);
            users.add(user);
        }
        return users;
    }


}
